<?php

class Ads_Model_Zipcode {

    private $_dbtable;

    /**
     * 
     */
    public function __construct() {
        $this->_dbtable = new Ads_Model_DbTable_Zipcode();
    }

    /**
     * 
     * @return type
     */
    public function fetchAll() {
        $select = $this->_dbtable->select();
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $id
     * @return type
     */
    public function getByPrimaryKey($id) {
        $select = $this->_dbtable->select();
        $select->where("id = ? ", $id);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $zipcode
     * @return type
     */
    public function getIdByZipcode($zipcode) {
        $select = $this->_dbtable->select();
        $select->where("codes = ? ", $zipcode);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $data
     */
    public function insert($data) {
        $this->_dbtable->insert($data);
    }

    /**
     * 
     * @param type $data
     * @param type $id
     */
    public function update($data, $id) {
        $this->_dbtable->update($data, "id = $id ");
    }

    /**
     * step1 : get city_id
     * @param type $zipId
     */
    public function getStateFromCityId($cityId) {
        $db = new Ads_Model_City();
        return $db->getStateNameFromCityId($cityId);
    }

    public function getStateNameFromZipObject($zip) {
        return $this->getStateFromCityId($zip[0]['city_id']);
    }

    public function getStateNameFromZipId($zipId) {
        $zip = $this->getByPrimaryKey($zipId);
        return $this->getStateNameFromZipObject($zip);
    }

}

